<?php

/* @project : 金钥匙
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 角色控制器
 */

namespace app\admin\controller;

use app\common\model\RoleAdmin;
use think\Request;

use app\common\model\Role as RoleModel;
use app\common\model\Menu as MenuModel;
use app\common\model\RoleMenu as RoleMenuModel;

class Role extends Base
{
    function index(Request $request)
    {
        // 设置默认排序规则为sort字段
        $request->_sort = 'sort asc';
        return $this->_index($request);
    }

    function add()
    {
        return $this->fetch();
    }

    function edit(Request $request)
    {
        $this->_edit($request);
        return $this->fetch();
    }

    function del(Request $request)
    {
        $result = $this->_del($request);

        if ($result !== false) {
            return $this->api_success('删除成功');
        } else {
            return $this->api_error('删除失败');
        }
    }

    function menu()
    {
        //权限组信息
        $role_model = new RoleModel;
        $role_list  = $role_model->getRoleList();

        $this->assign('role_list', $role_list);

        // 菜单信息
        $menu_model = new MenuModel;
        $menu_list  = $menu_model->getMenuList();
        $menu_list  = tree_to_list(list_to_tree($menu_list));

        $this->assign('menu_list', $menu_list);

        return $this->fetch();
    }

    function get_menu_list(Request $request)
    {
        $role_id         = $request->role_id;
        $role_menu_model = new RoleMenuModel();
        $menu_list       = $role_menu_model->getMenuListByRoleId($role_id);

        if ($menu_list !== false) {// 读取成功
            return $this->api_success('查询成功', $menu_list);
        }
    }

    function set_menu(Request $request)
    {
        // 删除之前的节点;
        $role_id = $request->role_id;

        $org_menu_list = $request->org_menu_list;

        $role_model = new RoleModel();
        $role_model->delMenuList($role_id, $org_menu_list);

        $menu_list = request()->param('menu_list');

        $role_menu_model = new RoleMenuModel();

        $result = $role_model->setMenuList($role_id, $menu_list);

        if (isset($request->admin)) {
            $role_menu_model->setAuth($role_id, $request->admin, 'admin');
        }

        if (isset($request->write)) {
            $role_menu_model->setAuth($role_id, $request->write, 'write');
        }
        if (isset($request->read)) {
            $role_menu_model->setAuth($role_id, $request->read, 'read');
        }

        if ($result === false) {
            $this->error('操作失败！');
        } else {
            $this->assign('jumpUrl', get_return_url());
            $this->success('操作成功！');
        }
    }

    function user()
    {
        $user_model = new \app\common\model\Admin();
        $user_list  = $user_model->getAdminList();
        $this->assign("user_list", $user_list);

        $role_model = new RoleModel();
        $role_list  = $role_model->getRoleList();
        $this->assign("role_list", $role_list);

        return $this->fetch();
    }

    function get_role_list(Request $request)
    {
        $user_id   = $request->user_id;
        $role_user = new RoleAdmin();
        $role_list = $role_user->getRoleListByAdminId($user_id);

        if ($role_list !== false) {// 读取成功
            return $this->api_success('查询成功', $role_list);
        }
    }

    function set_role(Request $request)
    {
        $role_model = new RoleModel();
        $role_model->delRoleByUserList($request->user_id);

        $result = $role_model->setRoleListByAdminList($request->role_id, $request->user_id);
        if ($result === false) {
            $this->error('操作失败！');
        } else {
            $this->assign('jumpUrl', get_return_url());
            $this->success('操作成功！');
        }
    }

}
